package io.louis.vmq.core;

import lombok.SneakyThrows;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public final class Kmq {

    public Kmq(String topic, int capacity) {
        this.topic = topic;
        this.capacity = capacity;
        this.list = new CopyOnWriteArrayList<KmqMessage>();
    }

    private String topic;

    private int capacity;

    private List<KmqMessage> list;

    private final Map<String, Integer> consumerMap = new ConcurrentHashMap<>(64);

    public boolean send(KmqMessage message) {
//        return queue.offer(message);
        return list.add(message);
    }
    public void addConsumer(String consumerName){
        consumerMap.put(consumerName,0);
    }

    @SneakyThrows
    public KmqMessage poll(String name, long timeout) {
        if(name == null || "".equals(name)){
            return null;
        }
        int val = consumerMap.get(name);
        if (val < list.size()){
            consumerMap.put(name,val+1);
            return list.get(val);
        }else{
            return null;
        }
    }
    public void submitOffset(String name, int offset){
        String key =  (name == null || "".equals(name)) ? "" : name;
    }
}
